package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;

import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping( "/roles")
@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    //查询所有角色
    public Result list( String name,String label,
                        @RequestParam( defaultValue = "1") Integer page,
                        @RequestParam (defaultValue = "10") Integer pageSize){
        log.info("查询所有角色");
        PageResult<Role> pageResult = roleService.list(name, label, page,pageSize);
        return Result.success(pageResult);
    }
    @DeleteMapping ("/{id}" )
    //根据id删除角色
    public Result deleteRole(@PathVariable("id") Integer id){
        log.info("删除角色");
        roleService.deleteRole(id);
        return Result.success();
    }
    @PostMapping
    //添加角色
    public Result addRole(@RequestBody Role role){
        log.info("添加角色");
        roleService.addRole(role);
        return Result.success();
    }
    @GetMapping ("/{id}" )
    public Result getRoleById(@PathVariable("id") Integer id){
        log.info("查询角色");
        return Result.success(roleService.getRoleById(id));
    }
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        log.info("更新角色");
        roleService.updateRole(role);
        return Result.success();
    }

    @GetMapping("/list")
    //查询所有角色,无请求参数
    public Result getAll() {
        log.info("查询所有角色");
        return Result.success(roleService.getAll());
    }
}
